import outdent from "outdent";
import * as Protocol from "vscode-languageserver-protocol";
import * as Types from "vscode-languageserver-types";
import * as LanguageServer from "../src/LanguageServer";

describe("textDocument/references", () => {
  let languageServer: LanguageServer.LanguageServer;

  function openDocument(source: string) {
    languageServer.sendNotification(
      Protocol.DidOpenTextDocumentNotification.type,
      {
        textDocument: Types.TextDocumentItem.create(
          "file:///test.ml",
          "ocaml",
          0,
          source,
        ),
      },
    );
  }

  async function query(position: Types.Position) {
    return await languageServer.sendRequest("textDocument/documentHighlight", {
      textDocument: Types.TextDocumentIdentifier.create("file:///test.ml"),
      position,
    });
  }

  beforeEach(async () => {
    languageServer = await LanguageServer.startAndInitialize();
  });

  afterEach(async () => {
    await LanguageServer.exit(languageServer);
  });

  it("highlight references in a file", async () => {
    openDocument(outdent`
      let num = 42
      let sum = num + 13
      let sum2 = sum + num
    `);

    const result = await query(Types.Position.create(0, 4));

    expect(result).toMatchInlineSnapshot(`
[
  {
    "kind": 1,
    "range": {
      "end": {
        "character": 7,
        "line": 0,
      },
      "start": {
        "character": 4,
        "line": 0,
      },
    },
  },
  {
    "kind": 1,
    "range": {
      "end": {
        "character": 13,
        "line": 1,
      },
      "start": {
        "character": 10,
        "line": 1,
      },
    },
  },
  {
    "kind": 1,
    "range": {
      "end": {
        "character": 20,
        "line": 2,
      },
      "start": {
        "character": 17,
        "line": 2,
      },
    },
  },
]
`);
  });
});
